﻿using MiniFox.Components;
using MiniFox.Data.EntityFramework;
using MiniFox.Infrastructure;
using Sys.Core.Models.Dto;
using Sys.Core.Models.Queries;
using Sys.Core.Models.VO;
using Sys.Core.Providers;

namespace Sys.Data
{
    [AutoResolve]
    public class SequenceProvider : EntityDataProvider<PlatformDbContext>, ISequenceProvider
    {
        private SequenceSetVo MappingToVo(SequenceSet entity)
        {
            if (entity == null) return null;
            return new SequenceSetVo
            {
                Name = entity.Name,
                Pattern = entity.Pattern,
                Prefix = entity.Prefix,
                Suffix = entity.Suffix,
                BaseOnUtc = entity.BaseOnUtc,
                CurrentVal = entity.CurrentVal,
                Length = entity.Length,
                MaxVal = entity.MaxVal,
                UpdateTime = entity.UpdateTime,
                Description = entity.Description,
            };
        }
        public ResultObject<IEnumerable<SequenceSetVo>> Query(PagedQueryObject<SequenceSetQuery> request)
        {
            var resultSet = this.PagedQueryExecute<SequenceSet>(request);
            return resultSet.GetResultObject(entity => new SequenceSetVo
            {
                Name = entity.Name,
                Pattern = entity.Pattern,
                Prefix = entity.Prefix,
                Suffix = entity.Suffix,
                BaseOnUtc = entity.BaseOnUtc,
                CurrentVal = entity.CurrentVal,
                Length = entity.Length,
                MaxVal = entity.MaxVal,
                UpdateTime = entity.UpdateTime,
                Description = entity.Description,
            });
        }

        public SequenceSetVo GetItem(string name)
        {
            var entity = this.CurrentDbContext.SequenceSet.SingleOrDefault(s => s.Name == name);
            if (entity == null) return null;
            return this.MappingToVo(entity);
        }

        public bool Create(SequenceSetDto dto)
        {
            var entity = new SequenceSet();
            entity.Name = dto.Name;
            entity.Pattern = dto.Pattern;
            entity.Prefix = dto.Prefix;
            entity.Suffix = dto.Suffix;
            entity.BaseOnUtc = dto.BaseOnUtc;
            entity.CurrentVal = 0;
            entity.Length = dto.Length;
            entity.MaxVal = dto.MaxVal;
            entity.Description = dto.Description;
            entity.UpdateTime = DateTime.Now;
            return this.Insert(entity) > 0;
        }

        public bool Update(SequenceSetDto dto)
        {
            var entity = this.CurrentDbContext.SequenceSet.SingleOrDefault(s => s.Name == dto.Name);
            if (entity == null) return false;

            entity.Pattern = dto.Pattern;
            entity.Prefix = dto.Prefix;
            entity.Suffix = dto.Suffix;
            entity.BaseOnUtc = dto.BaseOnUtc;
            entity.Length = dto.Length;
            entity.MaxVal = dto.MaxVal;
            entity.Description = dto.Description;
            entity.UpdateTime = DateTime.Now;
            return this.Update(entity) > 0;
        }

        public bool Remove(params string[] names)
        {
            List<object> list = new List<object>();
            foreach (var name in names)
            {
                var entity = this.CurrentDbContext.SequenceSet.SingleOrDefault(r => r.Name == name);
                list.Add(entity);
            }

            return this.Delete(list.ToArray()) > 0;
        }

        public bool Reset(params string[] names)
        {
            List<object> list = new List<object>();
            foreach (var name in names)
            {
                var entity = this.CurrentDbContext.SequenceSet.SingleOrDefault(r => r.Name == name);
                entity.CurrentVal = 0;
                entity.UpdateTime = DateTime.Now;
                list.Add(entity);
            }

            return this.Update(list.ToArray()) > 0;
        }
    }
}
